import { bold, yellow, Application, viewEngine, adapterFactory, renderHandlebars, send } from "./deps.ts";
// import { viewEngine, adapterFactory, renderHandlebars } from "./depsViewEngine.ts";
import arithmeticRouter from './router/pre/arithmetic.ts'
import combineRouters from './router/index.ts'
import logger from './middleware/logger.ts'
import responseTimer from './middleware/responseTime.ts'
import errorHandler from './middleware/errorHandler.ts'
import notFound from './middleware/notFound.ts'

const app = new Application();

// Logger
app.use(logger);

// Response Time
app.use(responseTimer);

// Error handler
app.use(errorHandler);

// view
const oakAdapter = adapterFactory.getOakAdapter();
app.use(
  viewEngine(oakAdapter, renderHandlebars, {
    viewRoot: "./view",
    viewExt: ".handlebars",
  })
);

app.use(combineRouters());

// Send static content
app.use(async (ctx) => {
  const root = `${Deno.cwd()}/static`
  console.log('root=>', root)
  await send(ctx, ctx.request.url.pathname, {
    root,
    index: "index.html"
  });
});

// app.use(async (ctx, next) => {
//   await next()
//   console.log('-=------===>', Deno.cwd())
//   ctx.render("index", { data: { name: "John" } });
// });

// A basic 404 page
app.use(notFound);

app.addEventListener("listen", ({ hostname, port }) => {
  console.log(
    bold("Start listening on ") + yellow(`${hostname}:${port}`),
  );
});

await app.listen("127.0.0.1:8000");

console.log(bold("Finished."));